package test2.shardingjdbc;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

import com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.TableRule;

public class App2 {
	
	private static DataSource createDataSource(final String dataSourceName) {
	    BasicDataSource result = new BasicDataSource();
	    result.setDriverClassName("net.sf.log4jdbc.DriverSpy");
	    result.setUrl("jdbc:log4jdbc:mysql://localhost:3306/" + dataSourceName);
	    result.setUsername("root");
	    result.setPassword("");
	    return result;
	}
	
	public static void main(String[] args) {
		Map<String, DataSource> dataSourceMap = new HashMap<>();
		dataSourceMap.put("ds_0", createDataSource("ds_0"));
//		dataSourceMap.put("ds_1", createDataSource("ds_1"));
//		真实的数据源可以使用任意一种数据库连接池，这里使用DBCP来举例
		
		DataSourceRule dataSourceRule = new DataSourceRule(dataSourceMap);
//		这里构造器需要一个入参：数据源名称与真实数据源之间的映射关系，这个关系的构造方法如下
		
		//使用了默认策略配置
		TableRule orderTableRule = new TableRule("t_order", Arrays.asList("t_order_0", "t_order_1", "t_order_2"), dataSourceRule);
//		ShardingRule shardingRule = new ShardingRule(dataSourceRule, Arrays.asList(orderTableRule),
	}

}
